mui.init();
mui.plusReady(function() {
	var ws = plus.webview.currentWebview(), 
		mobile_list = [],
		mobile_name = [],
		tel_address_list = [],
		friend_list_wrap = {},
		user_id = plus.storage.getItem('cg_user_id'),
		user_token = plus.storage.getItem('cg_user_token');
	ws.setBounce({ 
		position: {
			top: '344px'
		},
		changeoffset: {
			top: '0'
		}
	});
	getList();
	setTimeout(function() {
		var list = document.getElementById('list');
		window.indexedList = new mui.IndexedList(list);
		list.style.height = (document.body.offsetHeight - 44) + 'px';
	}, 1000);
	window.addEventListener('refresh', function(event) {
		//通过event.detail可获得传递过来的参数内容
		getList();
	});
	//进入用户详情页
	$('.address-list').delegate('.get-info', 'tap', function() {
		var friend_id = $(this).parent().attr('id');
		plus.storage.setItem('temp_friend_id', friend_id);
		mui.openWindow({
			url: 'friend_info_header.html',
			show: {
				autoShow: false, //页面loaded事件发生后自动显示，默认为true
				aniShow: 'slide-in-right', //页面显示动画，默认为”slide-in-right“；
				duration: '200' //页面动画持续时间，Android平台默认100毫秒，iOS平台默认200毫秒；
			}
		});
	});
	//搜索框获取焦点
	$('.input-click').focus(function(e){
			e.preventDefault();
			$('.mui-search').css('width','80%');
			$('.s-click').show();
	}).blur(function(){
		$('.mui-search').css({
			'width': '100%'
		});
		$('.s-click').hide();
	});
	//点击取消按钮
	$('.s-click').on('touchstart',function(e){
			e.preventDefault();
			$('.mui-search').css('width','100%');
			$('.input-click').blur();
			$('.s-click').hide();
	});
	//接受/添加按钮
	$('.address-list').delegate('.btn-accept', 'tap', function() {
		var _this = this,
			remark = $(this).prev('.new-friend-info').find('.remark').val(),
			to_user_id = $(this).parent().attr('id'),
			remark = $(this).parent().find('h1').text();
		add_flag = $(this).text() == '添加' ? true : false;
		if(add_flag) {
			mui.openWindow({
				url: 'send_check_info.html',
				extras: {
					'to_user_id': to_user_id,
					'remark': remark
				}
			});
		} else {
			//ajax请求接受好友
			myAjax('addresslist/front_user_relation/acceptFrineds', 'post', {
				user_id: user_id,
				user_token: user_token,
				to_user_id: to_user_id,
			}, function(data) {
				if(data.success) {
					$('.btn-accept').addClass('btn-accepted').removeClass('btn-accept');
				} else {
					mui.alert('添加好友失败', '提示');
				}
			});
		}
	});
	//好友列表渲染
	function getList() {
		plus.contacts.getAddressBook(plus.contacts.ADDRESSBOOK_PHONE, function(addressbook) {
			addressbook.find(["displayName", "phoneNumbers"], function(contacts) {
				mui.each(contacts, function(index, elem) {
					if(elem.phoneNumbers.length > 0) {
						mobile_list.push(elem.phoneNumbers[0].value.replace(/-/g, ''));
						mobile_name.push({
							'name': elem.displayName,
							'mobile': elem.phoneNumbers[0].value.replace(/-/g, '')
						})
					}
				});
				myAjax('addresslist/front_user_relation/compareFriendsData', 'post', {
					'user_id': user_id,
					'user_token': user_token,
					'mobile_list': mobile_list
				}, function(data) {
					if(data.success) {
						delete data.success;
						console.log(JSON.stringify(data))
						$.each(data, function(key, value) {
							for(var i = 0; i < value.length; i++) {
								var temp = [];
								for(var j = 0; j < mobile_name.length; j++) {
									if(value[i].mobile == mobile_name[j].mobile) {
										value[i].displayName = mobile_name[j].name;
									}
								}
							}
							tel_address_list.push({
								'letter': key,
								'friendinfo': value
							});
						});
						friend_list_wrap.friend_list = tel_address_list;
						friend_list_wrap.baseUrl = _baseUrl;
						var html = template('friend-list', friend_list_wrap);
						$('.address-list').append(html);
					} else {
						$('.mui-indexed-list-bar').hide();
						$('.mui-indexed-list-inner').append('<h6 class="mui-text-center">获取好友列表失败，请重试</h1>');
					}
					var page = plus.webview.getWebviewById('tel_address_list_header.html');
					mui.fire(page,'pageflowrefresh');
				});
			}, function() {
				mui.alert("通讯录获取失败请重试", '提示');
			}, {
				multiple: true
			});
		}, function(e) {
			plus.nativeUI.closeWaiting();
			mui.alert("通讯录获取失败请重试", '提示');
		});
	}
});